from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import loginDiag
import os
from dotenv import load_dotenv, find_dotenv,set_key

class Login(QtWidgets.QDialog,loginDiag.Ui_Dialog):
    sigLoginComplete = pyqtSignal([str,str, str, str, str, str])
    def __init__(self,db,parent=None):
        super(Login,self).__init__(parent)
        self.setupUi(self)
        self.btnLogin.clicked.connect(self.clicked_btnLogin)
        self.lePerson_id.returnPressed.connect(self.lePasswd.setFocus)
        self.lePasswd.returnPressed.connect(self.getCaFile)
        self.btnCafile.clicked.connect(self.getCaFile)
        self.envFile = find_dotenv()
        load_dotenv()
        self.lePerson_id.setText(os.getenv('PERSON_ID'))
        self.leCaFile.setText(os.getenv('CA_PATH'))   
        self.FeeTXF = os.getenv("FEE_TXF")
        self.FeeMXF = os.getenv("FEE_MXF")
        self.taxRatio = os.getenv("TAX_RATIO")
        self.setWindowIcon(QtGui.QIcon('stock_ticker.png'))

    def getCaFile(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        files, _ = QFileDialog.getOpenFileNames(self,"QFileDialog.getOpenFileNames()", "","憑證檔 (*.pfx)", options=options)
        #print(files)
        if files:
            self.leCaFile.setText(files[0])

    def clicked_btnLogin(self):
        self.sigLoginComplete.emit(self.lePerson_id.text(), self.lePasswd.text(), self.leCaFile.text(), self.FeeTXF, self.FeeMXF, self.taxRatio)
        set_key(self.envFile, 'PERSON_ID', self.lePerson_id.text())
        set_key(self.envFile, 'CA_PATH', self.leCaFile.text())
        self.close()
    
    